<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN""DTD/xhtml1-strict.dtd">
<%@page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@taglib uri="/WEB-INF/c.tld" prefix="c"%>
<%@taglib uri="/WEB-INF/fn.tld" prefix="fn"%>
<%@taglib uri="/WEB-INF/fmt.tld" prefix="fmt"%>
<%@taglib uri="/WEB-INF/el.tld" prefix="el"%>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
		<title>ACC670102B</title>
		<c:import url="/includes/css_inc.jsp"/>
		<c:import url="/includes/js_inc.jsp"/>
		<script>
			$(function() {
				$("#save").button({
		            icons: {
		                primary: "ui-icon-disk"
		            }
		        }).click(function() {
		        	if (validateEmpty()) {
		        		group0Chk();
			        	$("#saveForm").submit();
		        	}
		        });
				$("#back").button({
		            icons: {
		                primary: "ui-icon-arrowreturnthick-1-w"
		            }
		        }).click(function() {
		        	document.getElementById("job").value = "back";
		        	$("#saveForm").submit();
		        });
		    	$("#tabs").tabs();
			});
			
			function group0Chk() {
				var tabs = document.getElementById("tabs");
				var divs = tabs.getElementsByTagName("DIV");
				for (var i = 0; i < divs.length; i++) {
					var div = divs[i];
					var group0 = document.getElementById("authS" + div.id.substring(1, 5));
					group0.value = "0";
					var menu = div.getElementsByTagName("INPUT");
					var chkG0 = false;
					for (var x = 0; x < menu.length; x++) {
						var obj = menu[x];
						if (obj.type == "checkbox"){
							if (obj.id.indexOf("authS") >= 0) {
								if (obj.checked) {
									chkG0 = true;
									document.getElementById("authS" + obj.name).value = "1";
									var id00 = "authS" + obj.name.substring(0, obj.name.length - 2) + "00";
									if (document.getElementById(id00)) {
										document.getElementById(id00).value = "1";
									}
								} else {
									document.getElementById("authS" + obj.name).value = "0";
								}
							} else if (obj.id.indexOf("authI") >= 0) {
								if (obj.checked) {
									document.getElementById("authI" + obj.name).value = "1";
								} else {
									document.getElementById("authI" + obj.name).value = "0";
								}
							} else if (obj.id.indexOf("authU") >= 0) {
								if (obj.checked) {
									document.getElementById("authU" + obj.name).value = "1";
								} else {
									document.getElementById("authU" + obj.name).value = "0";
								}
							} else if (obj.id.indexOf("authD") >= 0) {
								if (obj.checked) {
									document.getElementById("authD" + obj.name).value = "1";
								} else {
									document.getElementById("authD" + obj.name).value = "0";
								}
							}
						}
					}
					if (chkG0) {
						group0.value = "1";
					}
				}
			}
			
			function setMenuAuthH(objH, type, id, group) {
				var mg = document.getElementsByTagName("INPUT");
				var tmpChk = objH.checked;
				for (var i = 0; i < mg.length; i++) {
					var obj = mg[i];
					if (type != "S") {
						if (obj.type == "checkbox" && (obj.id.indexOf("authS_" + group) >= 0 || obj.id.indexOf("auth" + type + "_" + group) >= 0)) {
							if (objH.checked) {
								obj.checked = true;
							} else {
								obj.checked = false;
							}
							setMenuAuth(obj, type, obj.id.split("_")[2], group, id);
						}
					} else {
						if (obj.type == "checkbox" && obj.id.indexOf("auth" + type + "_" + group) >= 0) {
							if (objH.checked) {
								obj.checked = true;
							} else {
								obj.checked = false;
							}
							setMenuAuth(obj, type, obj.id.split("_")[2], group, id);
						}
					}
				}
				if (tmpChk == false) {
					objH.checked = false;
					for (var i = 0; i < mg.length; i++) {
						var obj = mg[i];
						if (obj.type == "checkbox" && obj.id.indexOf("auth" + type + "_" + group) >= 0 && obj.id != objH.id) {
							if (obj.checked) {
								obj.checked = false;
							}
						}
					}
				}
			}
			
			function setMenuAuth(obj, type, id, group, parent) {
				if (type != "S") {
					if (obj.checked) {
						document.getElementById("authS_" + group + "_" + id).checked = true;
						document.getElementById("auth" + type + "_" + group + "_" + id).checked = true;
					} else {
						document.getElementById("authS_" + group + "_" + id).checked = true;
						document.getElementById("auth" + type + "_" + group + "_" + parent).checked = false;
					}
				} else {
					if (!obj.checked) {
						document.getElementById("authS_" + group + "_" + parent).checked = false;
						document.getElementById("authI_" + group + "_" + id).checked = false;
						document.getElementById("authI_" + group + "_" + parent).checked = false;
						document.getElementById("authU_" + group + "_" + id).checked = false;
						document.getElementById("authU_" + group + "_" + parent).checked = false;
						document.getElementById("authD_" + group + "_" + id).checked = false;
						document.getElementById("authD_" + group + "_" + parent).checked = false;
					}
				}
				if (obj.checked) {
					var mg = document.getElementsByTagName("INPUT");
					var pid = "auth" + type + "_" + group + "_" + parent;
					var objH = document.getElementById(pid);
					var chkAll = true;
					for (var i = 0; i < mg.length; i++) {
						var obj = mg[i];
						if (obj.type == "checkbox" && obj.id.indexOf("auth" + type + "_" + group) >= 0 && obj.id != pid) {
							if (!obj.checked) {
								chkAll = false;
								break;
							}
						}
					}
					if (chkAll) {
						objH.checked = true;
					}
				}
			}
		</script>
	</head>
	<body>
		<c:set var="authUser" value="${el:getMapValue(menuAuth, 'ACC670102')}"/>
		<c:import url="/apps/msg.jsp"/>
		<form class="acc67-form" name="saveForm" id="saveForm" action="${pageContext.request.contextPath}/acc67" method="post">
		<input type="hidden" name="app" id="app" value="ACC670102"/>
		<input type="hidden" name="job" id="job" value="${job}"/>
		<table width="100%" class="ui-widget ui-widget-content" border="0">
			<tr>
				<th align="left" class="acc67-header ui-widget-header" colspan="2"><b>${screenCode.ACC670102}</b></th>
			</tr>
			<tr>
				<td width="20%" class="bold">รหัสสิทธิ์ผู้ใช้งานระบบ : </td>
				<td>
				<c:if test="${empty(role.roleId)}">
				สร้างจากระบบ
				</c:if>
				<c:if test="${!empty(role.roleId)}">
				${role.roleId}
				<input type="hidden" name="roleId" id="roleId" value="${role.roleId}"/>
				</c:if>
				</td>
			</tr>
			<tr>
				<td class="bold">ชื่อสิทธิ์ผู้ใช้งานระบบ : </td>
				<td><input type="text" id="roleName" name="roleName" size="30" maxlength="50" value="${role.roleName}"/><span class="red" id="#roleName">*</span></td>
			</tr>
			<tr>
				<td colspan="2">
					<div id="tabs">
						<ul>
						<c:forEach var="menu0" items="${menus}">
						<c:if test="${menu0.menuGroup == '0'}">
							<input type="hidden" name="menuId" id="${menu0.menuId}" value="${menu0.menuId}">
							<input type="hidden" name="authS" id="authS${menu0.menuId}" value="0"/>
							<input type="hidden" name="authI" id="authI${menu0.menuId}" value="0"/>
							<input type="hidden" name="authU" id="authU${menu0.menuId}" value="0"/>
							<input type="hidden" name="authD" id="authD${menu0.menuId}" value="0"/>
							<li><a href="#T${menu0.menuId}" >${menu0.menuName}</a></li>
					    </c:if>
						</c:forEach>
						</ul>
						<c:forEach var="menuH" items="${menus}">
						<c:if test="${menuH.menuGroup == '0'}">
							<div id="T${menuH.menuId}">
								<table class="acc67-grid ui-widget ui-widget-content" width="100%" cellpadding="1" cellspacing="1">
						    	<tr>
									<th class="bold ui-state-default">ชื่อเมนู</th>
									<th class="bold ui-state-default" width="8%" align="center">เรียกดู</th>
									<th class="bold ui-state-default" width="8%" align="center">เพิ่ม</th>
									<th class="bold ui-state-default" width="8%" align="center">แก้ไข</th>
									<th class="bold ui-state-default" width="8%" align="center">ลบ</th>
								</tr>
						    	<c:forEach var="menuD" items="${menus}">
								<c:set var="auth" value="${el:getMapValue(roleAuth, menuD.menuId)}"/>
								<c:if var="authS" test="${fn:contains(auth, 'S')}"/>
								<c:if var="authI" test="${fn:contains(auth, 'I')}"/>
								<c:if var="authU" test="${fn:contains(auth, 'U')}"/>
								<c:if var="authD" test="${fn:contains(auth, 'D')}"/>
								<c:if test="${menuD.menuGroup != '0' && fn:startsWith(menuD.menuParent, menuH.menuId)}">
									<c:if test="${empty(menuD.menuUrl)}">
									<tr>
										<input type="hidden" name="menuId" id="${menuD.menuId}" value="${menuD.menuId}">
										<input type="hidden" name="authS" id="authS${menuD.menuId}" value="0"/>
										<input type="hidden" name="authI" id="authI${menuD.menuId}" value="0"/>
										<input type="hidden" name="authU" id="authU${menuD.menuId}" value="0"/>
										<input type="hidden" name="authD" id="authD${menuD.menuId}" value="0"/>
										<td class="bold ui-state-default">${menuD.menuName}</td>
										<td class="bold ui-state-default" align="center"><input type="checkbox" name="${menuD.menuId}" id="authS_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuthH(this, 'S', '${menuD.menuId}', '${menuD.menuGroup}');" value="1" ${authS ? 'checked' : ''}/></td>
										<td class="bold ui-state-default" align="center"><input type="checkbox" name="${menuD.menuId}" id="authI_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuthH(this, 'I', '${menuD.menuId}', '${menuD.menuGroup}');" value="1" ${authI ? 'checked' : ''}/></td>
										<td class="bold ui-state-default" align="center"><input type="checkbox" name="${menuD.menuId}" id="authU_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuthH(this, 'U', '${menuD.menuId}', '${menuD.menuGroup}');" value="1" ${authU ? 'checked' : ''}/></td>
										<td class="bold ui-state-default" align="center"><input type="checkbox" name="${menuD.menuId}" id="authD_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuthH(this, 'D', '${menuD.menuId}', '${menuD.menuGroup}');" value="1" ${authD ? 'checked' : ''}/></td>
									</tr>
									</c:if>
									<c:if test="${!empty(menuD.menuUrl)}">
									<tr>
										<input type="hidden" name="menuId" id="${menuD.menuId}" value="${menuD.menuId}">
										<input type="hidden" name="authS" id="authS${menuD.menuId}" value="0"/>
										<input type="hidden" name="authI" id="authI${menuD.menuId}" value="0"/>
										<input type="hidden" name="authU" id="authU${menuD.menuId}" value="0"/>
										<input type="hidden" name="authD" id="authD${menuD.menuId}" value="0"/>
										<td>${menuD.menuName}</td>
										<td align="center">&nbsp;<input type="checkbox" name="${menuD.menuId}" id="authS_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuth(this, 'S', '${menuD.menuId}', '${menuD.menuGroup}', '${menuD.menuParent}');" value="1" ${authS ? 'checked' : ''}/></td>
										<td align="center">&nbsp;<input type="checkbox" name="${menuD.menuId}" id="authI_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuth(this, 'I', '${menuD.menuId}', '${menuD.menuGroup}', '${menuD.menuParent}');" value="1" ${authI ? 'checked' : ''}/></td>
										<td align="center">&nbsp;<input type="checkbox" name="${menuD.menuId}" id="authU_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuth(this, 'U', '${menuD.menuId}', '${menuD.menuGroup}', '${menuD.menuParent}');" value="1" ${authU ? 'checked' : ''}/></td>
										<td align="center">&nbsp;<input type="checkbox" name="${menuD.menuId}" id="authD_${menuD.menuGroup}_${menuD.menuId}" onclick="setMenuAuth(this, 'D', '${menuD.menuId}', '${menuD.menuGroup}', '${menuD.menuParent}');" value="1" ${authD ? 'checked' : ''}/></td>
									</tr>
									</c:if>
							    </c:if>
								</c:forEach>
								</table>
							</div>
					    </c:if>
						</c:forEach>
				    </div>
				</td>
			</tr>
			<tr>
				<td colspan="2" align="center">
				<c:if test="${fn:contains(authUser, 'I') || fn:contains(authUser, 'U')}">
					<button id="save" type="button" class="acc67-button">บันทึกข้อมูล</button>
				</c:if>
				&nbsp;&nbsp;
				<button id="back" type="button" class="acc67-button">ย้อนกลับ</button>
				</td>
			</tr>
		</table>
		</form>
	</body>
</html>